package com.kiku.controller;

import cn.hutool.core.bean.BeanUtil;
import com.kiku.common.UserQuery;
import com.kiku.domain.po.User;
import com.kiku.dto.PageDTO;
import com.kiku.dto.UserFormDTO;
import com.kiku.service.UserService;
import com.kiku.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author kiku
 * @Date 2024/6/12 11:24
 * @Version 1.0
 */
@Api(tags = "用户管理接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("users")
public class UserController {

    private final UserService userService;

    @PostMapping
    @ApiOperation("新增用户")
    public void saveUser(@RequestBody UserFormDTO userFormDTO){
        // 1.转换DTO为PO
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        // 2.新增
        userService.save(user);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    public void removeUserById(@PathVariable("id") Long userId){
        userService.removeById(userId);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户")
    public UserVo queryUserById(@PathVariable("id") Long userId){
        return userService.queryUserAndAddressById(userId);
    }


    @GetMapping
    @ApiOperation("根据id集合查询用户")
    public List<UserVo> queryUserByIds(@RequestParam("ids") List<Long> ids){
        // 1.查询用户
        List<User> users = userService.listByIds(ids);
        // 2.处理vo
        return BeanUtil.copyToList(users, UserVo.class);
    }

    @GetMapping("/page")
    public PageDTO<UserVo> queryUsersPage(UserQuery query){
        return userService.queryUsersPage(query);
    }
}
